package com.copm.ifm.base.api;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.copm.ifm.base.basic.enums.DictCodeEnum;
import com.copm.ifm.base.basic.pojo.DictContentBO;
import com.copm.ifm.base.basic.util.AuthUtil;
import com.copm.ifm.common.redis.RedisKeyHelper;
import com.copm.ifm.common.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 字典字段填充工具类
 * <p>
 * 需手写对应字段的getter方法,like：
 * public class Test{
 * private Integer dictKey;
 * private String dictName;
 * <p>
 * public String getDictName(){
 * DictFieldFillUtil.getName(DictCodeEnum.TEST, dictKey, dictName);
 * }
 * }
 *
 * @author zzf
 * @date 2020/12/18 17:59
 */
@Slf4j
public class DictFieldFillUtil {

    public static final Set<String> ENABLE_CUSTOMIZED_KEY_SET = new HashSet<>(64);

    public static String getName(DictCodeEnum dictEnum, Integer key, String defaultVal) {
        if (StringUtils.isEmpty(defaultVal)) {
            if (ObjectUtil.isEmpty(key)) {
                return null;
            }
            try {
                return RedisServer.I.getNameById(dictEnum, key);
            } catch (Exception e) {
                log.error("序列化失败：" + e.getMessage());
                return null;
            }
        }
        return defaultVal;
    }

    public static String getNames(DictCodeEnum dictEnum, List<Integer> keyList, String defaultVal) {
        if (CollectionUtil.isEmpty(keyList)) {
            return null;
        } else {
            StringBuilder temp = new StringBuilder();
            for (Integer k : keyList) {
                String s = DictFieldFillUtil.getName(dictEnum, k, defaultVal);
                if (!StringUtils.isEmpty(s)) {
                    temp.append(StrUtil.isNotEmpty(temp) ? "，" + s : s);
                }
            }
            return temp.toString();
        }
    }

    /**
     * 使用枚举类单例
     */
    private enum RedisServer {

        /**
         * 实例对象
         */
        I;

        private String getSysDict(String code, String key) {
            Object o = RedisUtil.getInstance().hGetField(RedisKeyHelper.sysDict(code), key);
            DictContentBO dictContentBO = JSON.parseObject(String.valueOf(o), DictContentBO.class);
            return dictContentBO.currentUserLanguageName();
        }

        private String getSysTenantDict(String code, String key) {
            Object o = RedisUtil.getInstance()
                    .hGetField(RedisKeyHelper.sysTenantDict(AuthUtil.getCurrentUserTenant(), code), key);
            DictContentBO dictContentBO = JSON.parseObject(String.valueOf(o), DictContentBO.class);
            return dictContentBO.currentUserLanguageName();
        }

        public String getNameById(DictCodeEnum dictCodeEnum, Integer key) {
            String dictCode = null;
            try {
                dictCode = dictCodeEnum.getCode();
                log.info("++++++++++"+dictCodeEnum.toString());
            } catch (Exception e) {
                log.error("序列化失败：" + e.getMessage()+"++++++++"+dictCodeEnum);
            }
            if (ENABLE_CUSTOMIZED_KEY_SET.contains(dictCode)) {
                //允许租户自定义的需要在查询指定租户的字典数据
                return getSysTenantDict(dictCode, String.valueOf(key));
            } else {
                return getSysDict(dictCode, String.valueOf(key));
            }
        }
    }
}
